<template>
    <view class="content" v-if="isShow">
        <view class="box">
			<view class="item">
				<view>
					<text style="color: #ff0000;padding-right: 10rpx;">*</text>单位
				</view>
				<view class="item-r" @click="show = true">
					<input type="text" v-model="hospital" placeholder="请选择单位" class="inp" disabled>
					<u-icon name="arrow-right" color="#b3b3b3" size="28" class="u-m-l-10"></u-icon>
				</view>
			</view>
        	<view class="item">
        		<view>
        			<text style="color: #ff0000;padding-right: 10rpx;">*</text>姓名
        		</view>
        		<view class="item-r">
        			<input type="text" v-model="name" placeholder="请输入姓名" class="inp">
        		</view>
        	</view>
        	<view class="item">
        		<view>
        			<text style="color: #ff0000;padding-right: 10rpx;">*</text>手机号码
        		</view>
        		<view class="item-r">
        			<input type="number" v-model="tel" maxlength="11" placeholder="请输入手机号码" class="inp">
        		</view>
        	</view>
        </view>
		
		<view v-for="(item, index) in data" :key="index">
			<view class="title">{{item.name}}</view>
			<view class="box">
				<view class="item" v-for="(el, ind) in item.answer" :key="ind" @click="choose(item,el)">
					<image :src="el.isSelect ? '../../static/icon/choose_1.png' : '../../static/icon/choose_2.png'" class="choose-1"></image>
					<view class="answer">{{el.name}}</view>
				</view>
			</view>
		</view>
		
		<view class="btn" @click="doSubmit">提交问卷</view>
		
		<u-select v-model="show" mode="single-column" :list="list"
			value-name="id" label-name="name" @confirm="confirm"></u-select>
    </view>
</template>

<script>
	import {
	    ajaxPost,
	    isNull,
	    getSync,
	    getParam,
	    setSync
	} from '../../static/js/common.js'
    export default {
        data() {
            return {
				show: false,
                data: [],
				list: [],
				train_id: '',
				hospital: '',
				hospitalId: '',
				name: '',
				tel: '',
				isShow: false,
            };
        },
        onLoad(option) {
			console.log('option',option)
            if (option.train_id) {
				this.train_id = option.train_id
				this.getList()
				this.getStats()
			}
			
			this.getInfo()
			console.log('getInfo')
        },
		onShow() {
			console.log('onShow',this.$getSync('hospitalId'),getParam('code'),this.$getSync('userToken'))
			
			if (this.$getSync('hospitalId') && getParam('code')) {
				console.log('777')
				this.isShow = false
				uni.showLoading({
					title: '提交中...',
				})
				let code = getParam('code');
				this.code(code)
				uni.hideLoading()
			}
		},
        methods: {
            doSubmit() {
				if (!this.hospitalId) {
					this.$toast('请选择单位')
					return
				}
				if (!this.name) {
					this.$toast('请输入姓名')
					return
				}
				if (!this.tel) {
					this.$toast('请输入手机号码')
					return
				}
				if (!this.$checkPhone(this.tel)) {
					this.$toast('手机号码格式不正确');
					return
				}
				
				let code = '', tip = '', data = [], nums = '0'
				// 单位
				this.list.forEach(cur => {
					if (cur.id == this.hospitalId) {
						code = cur.coupon
						tip = cur.end_time
					}
				})
				// 选择题
				this.data.forEach(cur => {
					let a = ''
					cur.answer.forEach(el => {
						if (el.isSelect) {
							a = el.name
						}
						if (el.isSelect && el.nums !== undefined) {
							nums = el.nums
						}
					})
					
					data.push({
						id: cur.id,
						type: cur.type,
						name: cur.name,
						answer: a,
					})
				})
				
				if (!this.$getSync('userToken')) {
					this.$setSync('trainId',this.train_id)
					
					this.$setSync('hospitalId',this.hospitalId)
					this.$setSync('NAME',this.name)
					this.$setSync('TEL',this.tel)
					this.$setSync('DATA',data)
					this.$setSync('NUMS',nums)
					this.$setSync('TIP',tip)
					this.$setSync('CODE',code)
					
					this.login();
					return
				}
				
				this.$ajax('add_forms', {
				    userToken: this.$getSync('userToken'),
					train_id: this.train_id || this.$getSync('trainId'),
					train_stat_id: this.hospitalId,
					nickname: this.name,
					mobile: this.tel,
					questions: data,
					nums: nums,
				}).then(ret => {
					if (ret.status == 0) {
						if (nums > 0) {
							this.$cTo(`./settle?code=${code}&tip=${tip}&num=${nums}`)
						} else {
							this.$toast('提交成功')
							setTimeout(() => {
								this.$sTo(`/pages/tabbar/index`)
							}, 500)
						}
						this.$removeSync('trainId')
					} else {
						this.$toast(ret.message);
					}
				});
			},
			
			choose(item,el) {
				const nowStatu = el.isSelect
				item.answer.forEach(ii => {
					ii.isSelect = false;
				})
				el.isSelect =! nowStatu
			},
			
			confirm(e) {
				console.log(e)
				this.hospital = e[0].label
				this.hospitalId = e[0].value
			},
			
			getList() {
				this.$ajax('get_forms', {
			        userToken: this.$getSync('userToken'),
					train_id: this.train_id || this.$getSync('trainId'),
			    }).then(ret => {
					if (ret.status == 0) {
			            this.data = ret.data.questions
						this.data.forEach(cur => {
							cur.answer.forEach((el, index) => {
								this.$set(el, 'isSelect', false)
							})
						})
						
						if (!this.$getSync('hospitalId')) {
							this.isShow = true
						}
						
						console.log(this.data)
					} else {
						this.$toast(ret.message);
					}
				});
			},
			
			getStats() {
				this.$ajax('get_stats', {
			        userToken: this.$getSync('userToken'),
					train_id: this.train_id || this.$getSync('trainId'),
			    }).then(ret => {
					if (ret.status == 0) {
			            this.list = ret.data.stat
						this.list.forEach(cur => {
							this.$set(cur, 'isSelect', false)
						})
					} else {
						this.$toast(ret.message);
					}
				});
			},
			
			// 验证token是否有效，旧的会清掉
			getInfo() {
				this.$ajax('members', {
			        userToken: this.$getSync('userToken'),
			    }).then(ret => {
					if (ret.status == 0) {
			            
					} else {
						
					}
				});
			},
			
			login() {
			    const _this = this;
			    let url = window.location.href;
			    if (getParam('code')) {
			        let code = getParam('code');
			        _this.code(code)
					console.log('8888')
			    } else {
			        window.location.href =
			            'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx552129804846d230&redirect_uri=' +
			            encodeURIComponent(url) + '&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect';
						
			    }
			},
			
			code(code) {
				uni.showLoading({
					title: '提交中...',
				})
			    ajaxPost('weixin', {
			        code: code, //微信授权码
			    }).then(res => {
			        console.log('weixin', res)
			        if (res.status == 0) {
			            uni.setStorageSync('userToken', res.data.userToken); //存token
			            
						this.$ajax('add_forms', {
						    userToken: this.$getSync('userToken'),
							train_id: this.train_id || this.$getSync('trainId'),
							train_stat_id: this.$getSync('hospitalId'),
							nickname: this.$getSync('NAME'),
							mobile: this.$getSync('TEL'),
							questions: this.$getSync('DATA'),
							nums: this.$getSync('NUMS'),
						}).then(ret => {
							if (ret.status == 0) {
								if (this.$getSync('NUMS') > 0) {
									this.$cTo(`./settle?code=${this.$getSync('CODE')}&tip=${this.$getSync('TIP')}&num=${this.$getSync('NUMS')}`)
								} else {
									this.$toast('提交成功')
									setTimeout(() => {
										this.$sTo(`/pages/tabbar/index`)
									}, 500)
								}
								this.$removeSync('trainId')
								this.$removeSync('hospitalId')
								this.$removeSync('NAME')
								this.$removeSync('TEL')
								this.$removeSync('DATA')
								this.$removeSync('NUMS')
								this.$removeSync('TIP')
								this.$removeSync('CODE')
								
								console.log('参数',{
									userToken: this.$getSync('userToken'),
									train_id: this.train_id || this.$getSync('trainId'),
									train_stat_id: this.$getSync('hospitalId'),
									nickname: this.$getSync('NAME'),
									mobile: this.$getSync('TEL'),
									questions: this.$getSync('DATA'),
									nums: this.$getSync('NUMS'),
								})
							} else {
								this.$toast(ret.message);
							}
						});
						
						uni.hideLoading()
			        }
			    })
			},
        }
    };
</script>

<style lang="scss">
	
    page{
        background-color: #f5f5f5;
    }
	.content{
		padding: 30rpx 20rpx;
	}
	.box{
		padding: 0 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
	}
	.item{
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 0;
		border-bottom: 2rpx solid #e6e6e6;
	}
	.item:last-child{
		border-bottom: none;
	}
	.item-r{
		flex: 1;
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}
	.inp{
		flex: 1;
		text-align: right;
		padding-left: 20rpx;
		font-size: 28rpx;
	}
	
	.title{
		font-size: 28rpx;
		color: #536384;
		padding-bottom: 20rpx;
	}
	.choose-1{
		width: 35rpx;
		height: 35rpx;
		margin-right: 25rpx;
	}
	.answer{
		flex: 1;
		word-break: break-all;
		white-space: pre-line;
	}
	
	.btn{
		font-size: 30rpx;
		color: #fff;
		text-align: center;
		padding: 25rpx;
		margin: 40rpx 0;
		background: #01b8bd;
		border-radius: 50rpx;
	}

</style>
